<template>
  <div class="xtx-checkbox" @click="change()">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span v-if="$slots.default">
      <slot></slot>
    </span>
  </div>
</template>

<script setup>
// v-model ==> vue3.0 :modelValue   @update:modelValue
// 使用vueuse/core

import { useVModel } from '@vueuse/core'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})
const emit = defineEmits(['update:modelValue', 'change'])
const checked = useVModel(props, 'modelValue', emit)

const change = () => {
  const nVal = !checked.value
  checked.value = nVal // 可以直接修改
  emit('change', nVal)
}
</script>
<script>
export default {
  name: 'XtxCheckbox'
}
</script>
<style scoped lang='less'>
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;

  .icon-checked {
    color: @xtxColor;

    ~span {
      color: @xtxColor;
    }
  }

  i {
    position: relative;
    top: 1px;
  }

  span {
    margin-left: 2px;
  }
}
</style>
